/**
 * File Name	: BaseDaoImpl.java
 * Author		: adelwin
 * Create Date	: 13-Apr-2011:PM 11:43:18
 *
 * Copyright (c) 2011 adelwin. All Rights Reserved. <BR>
 * <BR>
 * This software contains confidential and proprietary information of
 * Adelwin. ("Confidential Information").<BR>
 * <BR>
 * Such Confidential Information shall not be disclosed and it shall
 * only be used in accordance with the terms of the license agreement
 * entered into with Solveware Independent; other than in accordance with the written
 * permission of Solveware Independent. <BR>
 *
 */
package com.uobgroup.cmt.base.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import com.uobgroup.cmt.base.dao.IBaseSourceDao;
import com.uobgroup.cmt.base.exception.BaseException;
import com.uobgroup.cmt.base.model.BaseModel;
import com.uobgroup.cmt.base.query.BaseQuery;

/**
 * @author adelwin
 *
 */
public class BaseSourceDaoImpl<M extends BaseModel> extends NamedParameterJdbcDaoSupport implements IBaseSourceDao<M> {
	protected Logger logger = Logger.getLogger(BaseSourceDaoImpl.class);
	private BaseQuery query;

	@Override
	public BaseQuery getQuery() {
		return query;
	}

	@Override
	public void setQuery(BaseQuery query) {
		this.query = query;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<M> getListByCriteria(M criteria) throws BaseException {
		logger.debug("creating sql parameter source");
		SqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(criteria);
		logger.debug("executing query");
		return (List<M>) this.getNamedParameterJdbcTemplate().query(this.query.getQuery(), sqlParameterSource, BeanPropertyRowMapper.newInstance(criteria.getClass()));
	}


}
